package api

import (
	"app/internal/controller/demo"
	"app/internal/model"
	"app/wd"
	"fmt"

	"github.com/gin-gonic/gin"
)

// RegisterAPI 注册demo路由
func registerDemo(r *gin.Engine) {
	r.GET("/ping", func(c *gin.Context) {
		data := map[string]interface{}{
			"message": "pong",
		}
		testMysql()
		testRedis(c)
		wd.Success(c, data)
		//panic(1111)
		//time.Sleep(1 * time.Second)
		//c.JSON(200, gin.H{
		//	"message": "pong",
		//})
	})

	r.GET("demo", demo.NewCtrl().Demo)
}

// testMysql 测试mysql
func testMysql() {
	//db := zz.App.Mysql["default"].DB.Table("tb_t1")
	user := model.TbDemo{}
	//sqlBuild := helper.SqlBuilder{
	//	Where:      "id=?",
	//	WhereParam: []interface{}{1},
	//	DB:         db,
	//}
	////err := sqlBuild.GetOne(&user)
	//if err != nil {
	//	fmt.Println("err:", err)
	//}
	fmt.Println("user:", user)
}

// testRedis 测试redis
func testRedis(c *gin.Context) {
	db := wd.App.Redis["default"]
	key := "test"
	val, err := db.DB.Get(c, key).Result()
	if err != nil {
		fmt.Println("err:", err)
	}
	val = "test"
	err = db.DB.Set(c, key, val, 0).Err()
	if err != nil {
		fmt.Println("err:", err)
	}
	fmt.Printf("val:%s\n", val)
}
